import cv2 as cv
import numpy as np


def img_resize(in_img,rate=1):
    tmp_img=in_img.copy()
    tmp_img=cv.resize(tmp_img,(int(in_img.shape[1]/rate),int(in_img.shape[0]/rate)))
    return tmp_img




# img=cv.imread('image/test/0.jpg')
# cv.imshow('img',img_resize(img,1))

def roto(img):

    hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV) #得到HSV
    # cv.imshow('hsv',img_resize(hsv))
    img_with_green=cv.inRange(hsv,np.array([35,43,46]),np.array([77,255,255])) #现在人被扣了，保留了背景绿幕
    img_without_green=cv.bitwise_not(img_with_green)  #反转
    img_without_green=cv.bitwise_and(img,img,mask=img_without_green)
    return img_without_green




cap=cv.VideoCapture(0,cv.CAP_DSHOW)
cap.set(3,960)#设置视频参数与参数
cap.set(4,960)
cap.set(cv.CAP_PROP_SATURATION, 50)  #设置饱和度
cap.set(cv.CAP_PROP_CONTRAST,40)  #设置对比度

# cap.set(cv.CAP_PROP_FRAME_WIDTH,1280)
# cap.set(cv.CAP_PROP_FRAME_HEIGHT,720)
cap.set(cv.CAP_PROP_FPS, 30)
cap.set(cv.CAP_PROP_BRIGHTNESS,120)

def beauty_face(img):
    dst = np.zeros_like(img)
    #int value1 = 3, value2 = 1; 磨皮程度与细节程度的确定
    v1 = 3
    v2 = 1
    dx = v1 * 5 # 双边滤波参数之一 
    fc = v1 * 12.5 # 双边滤波参数之一 
    p = 0.1
    temp4 = np.zeros_like(img)

    temp1 = cv.bilateralFilter(img,dx,fc,fc)
    temp2 = cv.subtract(temp1,img);
    temp2 = cv.add(temp2,(10,10,10,128))
    temp3 = cv.GaussianBlur(temp2,(2*v2 - 1,2*v2-1),0)
    temp4 = cv.add(img,temp3)
    dst = cv.addWeighted(img,p,temp4,1-p,0.0)
    dst = cv.add(dst,(10, 10, 10,255))
    return dst



flag=1 #设置一个标识，用来输出视频信息
while(cap.isOpened()):
    ret,frame=cap.read()
    frame=beauty_face(frame)  #美颜
    rotoed=roto(frame)  #扣绿幕
    cv.imshow("Frame",rotoed)
    k=cv.waitKey(1)&0xFF #每帧数据延迟1ms,延迟不能为0，否则读取的结果是静态帧
    if k ==ord('s'):
       print(cap.get(3))
       print(cap.get(4))
    elif k==ord('q'):
       break
cap.release()
cv.destroyAllWindows()